﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ruminate.GUI;
using Ruminate.GUI.Content;
using Ruminate.GUI.Framework;
using System.Diagnostics;

namespace SimpleGame
{

    /// <summary>
    /// This screen is for demonstrating the available widget and their capabilities. 
    /// </summary>
    public class MainUI : Screen  //building이 차후 다른곳에서 불러올 때 이름이 됨.
    {
        

        Gui _gui;
        GameMap gameMap;

        private bool is_open = false;
        private bool is_settingOn = false;
        private Widget buildingInfoUI;
        private Widget settingPanel;
        private Panel BottomPlate;
        private Panel NewsfeedPlate;
        List<Widget> BuildingInfoWidgets = new List<Widget>();
        List<Widget> NewsfeedWidgets = new List<Widget>();
        
        int NewsfeedWidth =200;
        int NewsfeedToggleWidth = 20;
        int BottomBarHeight=40;

        Button MainSettingBtn;
        Button MainRoomBtn;
        Button MainMapBtn;

        ToggleButton MapShowEmpty;

        public int showNewsfeedBar = 1;
        public bool hasMouse=false;

        int BtnHeight;

        public void toggleSetting()
        {
            Panel pt = new Panel(0, 0, 100, 100);

            if (is_settingOn)
            {
                _gui.RemoveWidget(settingPanel);
            }
            else
            {
                _gui.AddWidget(settingPanel);
            }
            is_settingOn = !is_settingOn;
        }

        public Tuple<Vector2, Vector2> getScreenRect()
        {
            Vector2 top_left;
            top_left.X = 0;
            top_left.Y = 0;

            Vector2 bottom_right;
            bottom_right.X = _game.screenSize.X;
            if (_game.gameMode == GameMode.room)
            {
            }
            else if (showNewsfeedBar > 0)
            {
                bottom_right.X -= NewsfeedWidth;
            }
            else if (showNewsfeedBar < 0)
            {
                bottom_right.X -= NewsfeedToggleWidth;
            }

            bottom_right.Y = _game.screenSize.Y - BottomBarHeight;

            return new Tuple<Vector2, Vector2>(top_left, bottom_right);

        }

        public void changeMode(bool isMap)
        {
            if (isMap)
            {
                _game.gameMode = GameMode.map;
                MainRoomBtn.Visible = true;
                MainMapBtn.Visible = false;
                NewsfeedPlate.Visible = true;
            }
            else
            {
                _game.gameMode = GameMode.room;
                MainRoomBtn.Visible = false;
                MainMapBtn.Visible = true;
                NewsfeedPlate.Visible = false;
            }
        }

        public override void Init(Game1 game)
        {

            _game = game;
            gameMap = _game.gameMap;
            var skin = new Skin(game.GreyImageMap, game.GreyMap);
            var skin2 = new Skin(game.GreyImageMap2, game.GreyMap2);
            var text = new Text(game.font12, Color.White);

            

            _gui = new Gui(game, skin, text);
            _gui.MouseDown += MouseDown;

            _gui.AddSkin("BigBtn", skin2);


            var tmpButton = new Button(0, 0, 100, "설");
            _gui.AddWidget(tmpButton);
            int BtnHeight = tmpButton.AbsoluteArea.Height - 1;
            _gui.RemoveWidget(tmpButton);

            

            Console.WriteLine(string.Format("버튼 높이 : {0}", BtnHeight));

            /* BottomPlate */
            BottomPlate = new Panel(0, (int)game.screenSize.Y - BottomBarHeight, (int)game.screenSize.X, BottomBarHeight);
            
            MainSettingBtn = new Button(0,0, 120, "설정", delegate
            {
                toggleSetting();
            });
            BottomPlate.AddWidget(MainSettingBtn);

            MainRoomBtn = new Button(120, 0, 120, "총장실", delegate
            {
                changeMode(false);
            });
            BottomPlate.AddWidget(MainRoomBtn);

            MainMapBtn = new Button(120, 0, 120, "캠퍼스", delegate
            {
                changeMode(true);
            });
            BottomPlate.AddWidget(MainMapBtn);

            MapShowEmpty = new ToggleButton(300, 0, 120, "빈 땅 표시");
            MapShowEmpty.OnToggle = delegate
            {
                gameMap.ShowEmptySpace = true;
            };
            MapShowEmpty.OffToggle = delegate{
                gameMap.ShowEmptySpace = false;
            };
            BottomPlate.AddWidget(MapShowEmpty);

            _gui.AddWidget(BottomPlate);

            /* NewsfeedPlate*/
            NewsfeedPlate = new Panel((int)_game.screenSize.X - NewsfeedWidth, 0, NewsfeedWidth, (int)_game.screenSize.Y - BottomBarHeight);
            
            _gui.AddWidget(NewsfeedPlate);

            var btn = new Button(0, 0, 192, "뉴스피드가 올라간다!\n누르면 창이뜬다!"){Skin = "BigBtn"};

            NewsfeedPlate.AddWidget(btn);
            
            /* SettingPanel */
            settingPanel = new Panel(0, (int)_game.screenSize.Y-BottomBarHeight-100, 120, 100);
            
            settingPanel.AddWidget(new Button(0, 0, 115, "저장"));
            settingPanel.AddWidget(new Button(0, BtnHeight, 115, "메인 화면"));
            settingPanel.AddWidget(new Button(0, BtnHeight*2, 115, "게임 종료"));
            


            //MainSettingBtn.Area.Y = 720 - MainSettingBtn.Area.Height;
            
            /*
            _gui.AddWidget(_testbutton = new Button(50, 50, 50, "TESTBUTTON", delegate{
                if (is_open == false)
                {
                    openBuildingPage(lec1);
                    is_open = true;
                }
                    
                else
                {
                    closeoption();
                }
            

            }));
            _gui.AddWidget(_testbutton2 = new Button(50, 100, 50, "Testbutton2", delegate
            {
                if (is_open == false)
                {
                    openBuildingPage(caf1);
                    is_open = true;
                }
                else
                {
                    closeoption();
                }


            }));
            _gui.AddWidget(_testbutton3 = new Button(50, 150, 50, "Testbutton2", delegate
            {
                if (is_open == false)
                {
                    openBuildingPage(caf2);
                    is_open = true;
                }
                else
                {
                    closeoption();
                }


            }));
            _gui.AddWidget(_mouseDown = new Label(10, 10, "Test"));*/

        }

        private void MouseDown(object sender, MouseEventArgs args)
        {

        }

        public override void OnResize()
        {
            //_gui.

        }

        public override bool Update(GameTime gameTime)
        {
            _gui.Update();
            hasMouse = _gui.HasMouse;
            return false;
        }

        public override void Draw(GameTime gameTime)
        {
            
            _gui.Draw();
            //sBatch.Begin();
            //sBatch.Draw(beaker, new Vector2(100, 50), Color.White);
            //sBatch.End();

        }

        protected void openBuildingCafeteriaPage(BuildingCafeteria bd)
        {

            string name = bd.name;
            int rank = bd.upgrade_rank;
            int condition = bd.condition;
            int month = bd.month;

            BuildingInfoWidgets.Clear();

            BuildingInfoWidgets.Add( new Label(0,0,string.Format("이름 : {0}({1})",name,bd.GetType().Name)));
            BuildingInfoWidgets.Add(new Label(0, 25, string.Format("증축 단계 : {0}", rank)));
            BuildingInfoWidgets.Add(new Label(0, 50, string.Format("사용 개월 : {0}", month)));
            BuildingInfoWidgets.Add(new Label(0, 75, string.Format("내구도 : {0}%", condition)));

            buildingInfoUI = new Panel(70, 70, 300, 300)
            {
                Children = BuildingInfoWidgets.ToArray()
            };
            _gui.AddWidget(buildingInfoUI);
        }

        protected void openBuildingLabPage(BuildingLab bd)
        {
            string name = bd.name;
            int rank = bd.upgrade_rank;
            int condition = bd.condition;
            int month = bd.month;

            BuildingInfoWidgets.Clear();

            buildingInfoUI = new Panel(70, 70, 300, 300);
            

            BuildingInfoWidgets.Add(new Label(0, 0, string.Format("이름 : {0}({1})", name, bd.GetType().Name)));
            BuildingInfoWidgets.Add(new Label(0, 25, string.Format("증축 단계 : {0}", rank)));
            BuildingInfoWidgets.Add(new Label(0, 50, string.Format("사용 개월 : {0}", month)));
            BuildingInfoWidgets.Add(new Label(0, 75, string.Format("내구도 : {0}%", condition)));
            BuildingInfoWidgets.Add(new Button(buildingInfoUI.Area.Width-100,buildingInfoUI.Area.Height-40,80,"닫기",delegate
            {
                closeOption();
            }));
            
            buildingInfoUI.AddWidgets(BuildingInfoWidgets.ToArray());

            _gui.AddWidget(buildingInfoUI);
        }

        protected void openBuildingLecturePage(BuildingLecture bd)
        {
            string name = bd.name;
            int rank = bd.upgrade_rank;
            int condition = bd.condition;
            int month = bd.month;

            BuildingInfoWidgets.Clear();

            BuildingInfoWidgets.Add(new Label(0, 0, string.Format("이름 : {0}({1})", name, bd.GetType().Name)));
            BuildingInfoWidgets.Add(new Label(0, 25, string.Format("증축 단계 : {0}", rank)));
            BuildingInfoWidgets.Add(new Label(0, 50, string.Format("사용 개월 : {0}", month)));
            BuildingInfoWidgets.Add(new Label(0, 75, string.Format("내구도 : {0}%", condition)));

            buildingInfoUI = new Panel(70, 70, 300, 300)
            {
                Children = BuildingInfoWidgets.ToArray()
            };
            _gui.AddWidget(buildingInfoUI);
        }

        public void openBuildingPage(Building bd)
        {
            Type bdType = bd.GetType();

            if (is_open)
            {
                closeOption();
            }

            if (bdType == typeof(BuildingLecture))
            {
                this.openBuildingLecturePage((BuildingLecture)bd);
            }
            else if (bdType == typeof(BuildingLab))
            {
                this.openBuildingLabPage((BuildingLab)bd);
            }
            else if (bdType == typeof(BuildingCafeteria))
            {
                this.openBuildingCafeteriaPage((BuildingCafeteria)bd);
            }
            else if (bdType == typeof(BuildingEmpty))
            {
                //음
            }
            else
            {
                Debug.Assert("타입 캐스팅 안되는 빌딩 타입" == null);
            }

            
            is_open = true;
        }

        public void closeOption()
        {
            _gui.RemoveWidget(buildingInfoUI);

            is_open = false;
        }

    }
}
